package day3.LC329;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
//        String str = new String("[[7,7,5],[2,4,6],[8,2,0]]").replaceAll("\\[", "\\{")
//                .replaceAll("\\]", "\\}");
        int[][] matrix = {{7,7,5}, {2,4,6}, {8,2,0}};
        int target = solution.longestIncreasingPath(matrix);
        System.out.println(target);
    }
}

class Solution {
    public int longestIncreasingPath(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int max = 0;//记录最大可以移动的格数

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int[][] result = new int[m][n];//以(i,j)为起点，移动，
//                int count = 0;//记录步数
                result[i][j] = 1;
                int count = 1;
                // 从(i,j)开始，向右，向下，向右下移动
                count=keep(matrix, result, i, j, count, m, n);
                max = Math.max(max, count);
            }
        }
        return max;
    }

    private static int keep(int[][] arr, int[][] result, int i, int j, int count, int m, int n) {
        int temp1=0;
        int temp2=0;
        int temp3=0;
        int temp4=0;

        if (j + 1 < n && arr[i][j + 1] - arr[i][j] >0 && result[i][j + 1] == 0) {
            int temp = count;
            result[i][j + 1] = 1;
            temp++;
            temp=keep(arr, result, i, j + 1, temp, m, n);
            return temp;
        }else
        if (i + 1 < m && arr[i + 1][j] - arr[i][j]>0 && result[i + 1][j] == 0) {
            int temp = count;
            result[i + 1][j] = 1;
            temp++;
            temp=keep(arr, result, i + 1, j, temp, m, n);
            return temp;
        }else
        if (j - 1 >= 0 && arr[i][j - 1] - arr[i][j]>0 && result[i][j - 1] == 0) {
            int temp = count;
            result[i][j - 1] = 1;
            temp++;
            temp=keep(arr, result, i, j - 1, temp, m, n);
            return  temp;
        }else
        if (i - 1 >= 0 && arr[i - 1][j] - arr[i][j]>0 && result[i - 1][j] == 0) {
            int temp = count;
            result[i - 1][j] = 1;
            temp++;
            temp=keep(arr, result, i - 1, j, temp, m, n);
            return temp;
        }else {
            return count;
        }
//        return Math.max(temp1,Math.max(temp2,Math.max(temp3,temp4)));
    }
}
